package org.apache.commons.tfs;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;

import net.pws.common.computer.ComputerHardwarable;
import net.pws.common.computer.ComputerHardwareFactory;

public class DefaultLicence implements Licence {

	private static final SimpleDateFormat DATE_FOMAT = new SimpleDateFormat(
			"yyyyMMdd");

	// 上次检验日期
	private String validateDate;

	private boolean validate = false;

	private String name;

	private String code;

	private String version;

	private VersionType versionType;

	private String copyright;

	private Date sign;

	private Date expiry;

	private String unit;

	private String macs;

	private String cpus;

	private String board;

	private String disks;

	private int userLimit = 0;

	private String errorMessage;

	private String toString(Date date) {
		if (date == null) {
			return "";
		}
		return DATE_FOMAT.format(date);
	}

	@Override
	public boolean validate() {
		if (validateDate != null && validateDate.equals(toString(new Date()))) {
			return validate;
		}

		if (sign == null) {
			unvalid(MSG_UNVALID);
			return false;
		}

		if (sign.after(new Date())) {
			unvalid(MSG_UNVALID);
			return false;
		}

		if (expiry != null && expiry.before(new Date())) {
			unvalid(MSG_EXPIRY);
			return false;
		}

		ComputerHardwarable hardwarable = null;
		try {
			hardwarable = ComputerHardwareFactory.getInstance()
					.getComputerHardware();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (hardwarable == null) {
			unvalid(MSG_HARDWARE);
			return false;
		}

		if (StringUtils.isNotBlank(board)
				&& !board.equals(hardwarable.getBoard())) {
			unvalid(MSG_HARDWARE);
			return false;
		}

		if (!validateHardware(cpus, hardwarable.getCpus())) {
			unvalid(MSG_HARDWARE);
			return false;
		}

		if (!validateHardware(disks, hardwarable.getDisks())) {
			unvalid(MSG_HARDWARE);
			return false;
		}

		if (!validateHardware(macs, hardwarable.getMacs())) {
			unvalid(MSG_HARDWARE);
			return false;
		}

		validate = true;
		errorMessage = null;
		validateDate = toString(new Date());
		return validate;
	}

	private void unvalid(String msg) {
		validate = false;
		errorMessage = msg;
		validateDate = toString(new Date());
	}

	private boolean validateHardware(String licence, String[] hardwares) {
		if (StringUtils.isBlank(licence)) {
			return true;
		}

		if (hardwares == null || hardwares.length == 0) {
			return false;
		}

		for (String hardware : hardwares) {
			if (licence.indexOf(hardware) >= 0) {
				return true;
			}
		}

		return false;
	}

	@Override
	public String getErrorMessage() {
		return errorMessage;
	}

	@Override
	public Date getSign() {
		return sign;
	}

	@Override
	public String getUnit() {
		return unit;
	}

	@Override
	public String getMacs() {
		return macs;
	}

	@Override
	public Date getExpiry() {
		return expiry;
	}

	@Override
	public int getUserLimit() {
		return userLimit;
	}

	public void setSign(Date sign) {
		this.sign = sign;
	}

	public void setExpiry(Date expiry) {
		this.expiry = expiry;
	}

	public void setUnit(String unit) {
		this.unit = unit;
	}

	public void setUserLimit(int userLimit) {
		this.userLimit = userLimit;
	}

	public String getCpus() {
		return cpus;
	}

	public void setCpus(String cpus) {
		this.cpus = cpus;
	}

	public String getBoard() {
		return board;
	}

	public void setBoard(String board) {
		this.board = board;
	}

	public String getDisks() {
		return disks;
	}

	public void setDisks(String disks) {
		this.disks = disks;
	}

	public void setMacs(String macs) {
		this.macs = macs;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getVersion() {
		return version;
	}

	public void setVersion(String version) {
		this.version = version;
	}

	public VersionType getVersionType() {
		return versionType;
	}

	public void setVersionType(VersionType versionType) {
		this.versionType = versionType;
	}

	public String getCopyright() {
		return copyright;
	}

	public void setCopyright(String copyright) {
		this.copyright = copyright;
	}

	@Override
	public boolean isFormalType() {
		if (versionType == VersionType.Develop
				|| versionType == VersionType.Test
				|| versionType == VersionType.Trial
				|| versionType == VersionType.Unknown) {
			return false;
		}
		return true;
	}
}
